#!/usr/bin/env node
const program = require('commander');
const chalk = require('chalk');
const figlet = require('figlet');
const CommandReplace = require('./replace')
const CommandJstack = require('./jstack')

// banner
figlet('SUPER JAER', function(err, data) {
    if (err) {
        return;
    }
    console.log(chalk.hex('#A9BED5').bold(data));
    
    program.version('1.0.4');
    program.command("replace").alias('rp')
    .description('Replace class in jar file with command 【jar】')
    .requiredOption('-s, --source [source]', 'Source jar file path')
    .requiredOption('-r, --resource [resource]', 'Resource absoulte path!')
    .option('-d, --detail', 'Whether record the detail msg to file')
    .option('-m, --message [message]', 'Human readable message for recording the replacement info or others sth..?')
    .action((options) => {
        commandReplaceFunc(options);
    });
    program.command('jstack').alias('jsk')
    .description('Help get thred stack info to file')
    .requiredOption('-p, --pid [pid]', 'Pid of your java program!')
    .requiredOption('-i --interval [number]', 'Interval time 【second】!')
    .requiredOption('-t --total [number]', 'Total times for jstack!')
    .requiredOption('-d --dir [dir]', 'Log file local storage dir!')
    .option('-n --name [name]', 'Jstack log file prefix!')
    .action((options) => {
        commandJstack(options)
    })
    program.parse(process.argv);
});

function commandReplaceFunc(options) {
    CommandReplace.step1Init(options);
}

function commandJstack(options) {
    try{
        CommandJstack.CommandJstack.step1Init(options);
    }catch(err) {
        console.log(err);
    }
}
